<template>
    <div class="commount">
        <el-form 
        :model="form" 
        :rules="rules" 
        ref="ruleForm" 
        label-width="100px" 
        class="demo-ruleForm">
            <el-form-item label="手机号码" prop="mobile" class="margintop20">
                <el-input v-model="form.mobile" ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" class="margintop20">
                <el-input type="password" v-model="form.password" show-password></el-input>
            </el-form-item>
            <!-- <div class="aa " >
                 <span style="color:red">*</span><p>登录身份</p>
              <el-radio v-model="radio" label="1">学生身份</el-radio>
                <el-radio v-model="radio" label="2">讲师身份</el-radio>
            </div> -->
           
            <el-form-item class="margintop20">
                <el-button type="primary" class="width100" @click="loginTodo">立即登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>


<script>
import {signinAjax} from "@/api"
  export default {
    data(){
      return{
          form:{mobile:""},
          rules:{   
              mobile:[
                  {required:true,message:"请输入手机号码",trigger:'blur'},
                  {
                        validator:(rule, value, callback)=>{
                            var mobileReg = /^1(3|5|6|7|8|9)\d{9}$/
                            if(mobileReg.test(value)){
                                callback()
                            }else{
                                callback(new Error("请输入正确的手机号码"));
                            }
                        },
                        trigger:'change'
                    }
              ],
              password:[
                    {required:true,message:"请输入登录密码",trigger:'blur'},
                    {
                        validator:(rule, value, callback)=>{
                            var passReg = /^[A-Za-z0-9]{4,12}$/
                            if(value.length<4){
                                callback(new Error("密码长度至少4位"));
                            }else if(value.length>12){
                                callback(new Error("密码长度最多12位"));
                            }else if(!passReg.test(value)){
                                callback(new Error("请输入正确的密码"));
                                
                            }else{
                                 callback()
                            }
                        },
                        trigger:'change'
                    }
                ]
          },
          radio: '1'
      }
    },
    methods: {
        loginTodo(){
            this.$refs.ruleForm.validate(valid=>{
                if(valid){
                    console.log(this.form)
                    signinAjax(this.form)
                    .then(res=>{
                        if(res.type){
                            sessionStorage.setItem("token",res.token)
                            localStorage.setItem("mobile",this.form.mobile)
                            this.$router.push({name:'index'})  //登录成功进入主页
                            
                        }
                       
                    })
                    .catch(err=>{
                        console.log(333333)
                    })
                }else{
                    console.log("error")
                    return false
                }
            })
        }
     },
     mounted(){
         if(localStorage.getItem("mobile")){
             this.form.mobile = localStorage.getItem("mobile")
         }
     }
  }
</script>



<style lang="scss" scoped>
.demo-ruleForm{
    padding-right: 50px;
}
.aa{
    margin-left: 40px;
    display: flex;
    flex: 1;
   justify-items: center;
    align-items: center;
    margin-bottom: 20px;
   
    p{
        margin-right: 20px;
        font-size: 14px;
    }
}

.width100{
    width: 100%;
}

</style>